//
//  AlertManager.h
//  UUZuche
//
//  Created by phenmod on 13-4-12.
//

#import "AlertRelationManager.h"
#import "AlertRelation.h"
//#import "BaseViewController.h"
#import "TXiOSDemoApp-Swift.h"

@interface AlertRelationManager ()

@property (nonatomic, strong) NSMutableArray *arrayRelation;
@property (nonatomic, strong) BaseViewController *currentPopVC;

@end

static AlertRelationManager *globalAlertManager = nil;


@implementation AlertRelationManager

@synthesize arrayRelation = _arrayRelation;

// 获取数据管理的控制器
+ (AlertRelationManager *)getInstance
{
    @synchronized(self)
    {
        // 实例对象只分配一次
        if (globalAlertManager == nil)
        {
            globalAlertManager = [[super allocWithZone:NULL] init];

            globalAlertManager.arrayRelation = [[NSMutableArray alloc] initWithCapacity:0];
        }
    }

    return globalAlertManager;
}

+ (id)allocWithZone:(NSZone *)zone
{
    return [self getInstance];
}

- (id)copyWithZone:(NSZone *)zone
{
    return self;
}

+ (void)appendWithBaseNameVC:(BaseViewController *)baseNameVC andAlertView:(UIAlertView *)alertView
{
    AlertRelation *alertViewRelation = [[AlertRelation alloc] init];

    [alertViewRelation setAlertView:alertView];
    [alertViewRelation setBaseNameVC:baseNameVC];

    [[[AlertRelationManager getInstance] arrayRelation] addObject:alertViewRelation];
}

+ (void)closeAlertViewWithBaseNameVC:(BaseViewController *)baseNameVC
{
    NSArray *arrayRelation = [[AlertRelationManager getInstance] arrayRelation];

    // 要被移除的对象
    NSMutableArray *arrayRemoved = [[NSMutableArray alloc] init];

    for (AlertRelation *alertViewRelation in arrayRelation)
    {
        if ([alertViewRelation.baseNameVC isEqual:baseNameVC])
        {
            [alertViewRelation.alertView setDelegate:nil];
            [alertViewRelation.alertView dismissWithClickedButtonIndex:0 animated:NO];

            // 添加到被移除对象数组中
            [arrayRemoved addObject:alertViewRelation];
        }
    }

    // 移除对象
    [[[AlertRelationManager getInstance] arrayRelation] removeObjectsInArray:arrayRemoved];
}

@end
